package com.itheima.mp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.itheima.mp.domain.dto.PageDTO;
import com.itheima.mp.domain.po.Address;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.domain.query.UserQuery;
import com.itheima.mp.domain.vo.AddressVO;
import com.itheima.mp.domain.vo.UserVO;
import com.itheima.mp.enums.UserStatus;
import com.itheima.mp.mapper.UserMapper;
import com.itheima.mp.service.IuserService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ajin
 * @create 2024-07-25 22:02
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IuserService {
    @Override
    public void deductBalance(Long id, Integer money) {
        // 1.查询用户
        User user = getById(id);
        // 2.判断用户状态
        if (user == null || user.getStatus() == UserStatus.FREEZE) {
            throw new RuntimeException("用户状态异常");
        }
        // 3.判断用户余额
        if (user.getBalance() < money) {
            throw new RuntimeException("用户余额不足");
        }
        // 4.扣减余额
        int remainBalance = user.getBalance() - money;
        lambdaUpdate()
                .set(User::getBalance, remainBalance) // 更新余额
                .set(remainBalance == 0, User::getStatus, UserStatus.FREEZE) // 动态判断，是否更新status
                .eq(User::getId, id)
                .eq(User::getBalance, user.getBalance()) // 乐观锁
                .update();
    }

    @Override
    public UserVO queryUserAndAddressById(Long userId) {
        User user = getById(userId);
        if(user == null){
            throw new RuntimeException("用户不存在");
        }
        List<Address> addressList = Db.lambdaQuery(Address.class)
                .eq(Address::getUserId, userId)
                .list();
        UserVO userVO = BeanUtil.copyProperties(user, UserVO.class);
        List<AddressVO> addressVOList = BeanUtil.copyToList(addressList, AddressVO.class);
        userVO.setAddresses(addressVOList);
        return userVO;
    }

    @Override
    public PageDTO<UserVO> queryUsersPage(UserQuery query) {
        // 1.构建条件
        Page<User> page = query.toMpPage();
        // 2.查询
        page(page);
        // 3.封装返回
        return PageDTO.of(page, UserVO.class);
//        Page<User> page = Page.of(query.getPageNo(), query.getPageSize());
//        if (query.getSortBy() != null) {
//            page.addOrder(new OrderItem(query.getSortBy(), query.getIsAsc()));
//        }else{
//            page.addOrder(new OrderItem("update_time",false));
//        }
//        Page<User> p = lambdaQuery().like(query.getName()!=null,User::getUsername, query.getName())
//                .eq(query.getStatus()!=null,User::getStatus, query.getStatus())
//                .page(page);
//        PageDTO<UserVO> dto = new PageDTO<>();
//        dto.setTotal(p.getTotal());
//        dto.setPages(p.getPages());
//        dto.setList(BeanUtil.copyToList(p.getRecords(), UserVO.class));
//        return dto;
    }
}
